import { createApp } from 'vue'

import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

import App from './App.vue'
import './style.css'

import pinia from './store/store'
import router from './router/index.js'
import useAdminStore from '@/store/index.js'

import 'element-plus/theme-chalk/el-loading.css'
import 'element-plus/theme-chalk/el-message.css'

const app = createApp(App)
app.use(router)
app.use(pinia)
app.use(mavonEditor)
app.mount('#app')

router.beforeEach(async (to, from, next) => {
	const adminStore = useAdminStore(pinia)
	if (!sessionStorage.getItem('token') && to.path !== '/login') {
		next('/login')
	} else {
		if (to.path === '/login') {
			sessionStorage.clear()
		} else {
			if (Object.keys(adminStore.userInfo).length === 0) {
				adminStore.userInfo = await adminStore.setUserInfo()
			}
			if (adminStore.menu.length === 0) {
				adminStore.menu = await adminStore.setMenu()
			}
		}
		next()
	}
})
